﻿using Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DearInformation.Controllers
{
    public class ScheduleController : Controller
    {

        public ActionResult CheckSession()
        {
            User u = Session["user"] as User;
            User other = Session["otheruser"] as User;
            u = null;
            if (u == null || other == null)
            {
                return RedirectToAction("Index","Home");
            }
            return null;
        }

        //
        // GET: /Schedule/
        public ActionResult MyScheduleList()
        {
            return View();
        }

        public ActionResult NewSchedule()
        {
            return View();
        }

        public ActionResult EditMySchedule()
        {
            string id = Request["id"] ?? string.Empty;
            ScheduleServer server = new ScheduleServer();
            Schedules sch = server.GetScheduleById(id);

            ViewData["tag"] = Request["tag"] ?? string.Empty;

            return View(sch);
        }

        public ActionResult MyScheduleInfo()
        {
            string id = Request["id"] ?? string.Empty;
            ScheduleServer server = new ScheduleServer();
            Schedules sch = server.GetScheduleById(id);
            ViewData["tag"] = Request["tag"] ?? string.Empty;
            return View(sch);
        }

        public ActionResult ApproveSchedule()
        {
            string id = Request["id"] ?? string.Empty;
            ScheduleServer server = new ScheduleServer();
            Schedules sch = server.GetScheduleById(id);
            ViewData["tag"] = Request["tag"] ?? string.Empty;
            return View(sch);
        }

        public JsonResult GetMyUnfinishSchedules()
        {
            CheckSession();
            ScheduleServer server = new ScheduleServer();

            User u = Session["user"] as User;

            List<Schedules> list = server.GetMyUnfinishSchedules(u.Uid);

            if (list == null || list.Count == 0)
            {
                return Json(Json(new List<Schedules>()), JsonRequestBehavior.AllowGet);
            }
            return Json(Json(list), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetMyPeddingSchedules()
        {
            CheckSession();
            ScheduleServer server = new ScheduleServer();

            User u = Session["user"] as User;

            List<Schedules> list = server.GetMyPeddingSchedules(u.Uid);

            if (list == null || list.Count == 0)
            {
                return Json(Json(new List<Schedules>()), JsonRequestBehavior.AllowGet);
            }
            return Json(Json(list), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetMyFinishSchedules()
        {
            CheckSession();
            ScheduleServer server = new ScheduleServer();

            User u = Session["user"] as User;

            List<Schedules> list = server.GetMyFinishSchedules(u.Uid);

            if (list == null || list.Count == 0)
            {
                return Json(Json(new List<Schedules>()), JsonRequestBehavior.AllowGet);
            }
            return Json(Json(list), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetMyApprovechedules()
        {
            CheckSession();
            ScheduleServer server = new ScheduleServer();

            User u = Session["user"] as User;

            List<Schedules> list = server.GetMyApproveSchedules(u.Uid);

            if (list == null || list.Count == 0)
            {
                return Json(Json(new List<Schedules>()), JsonRequestBehavior.AllowGet);
            }
            return Json(Json(list), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult NewSchedule(Schedules schedule)
        {
            CheckSession();
            User u = Session["user"] as User;
            User other = Session["otheruser"] as User;

            Schedules sch = schedule;
            sch.SendUid = u.Uid;
            sch.ApproveUid = other.Uid;
            sch.BeginDate = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
            sch.ApproveDate = "";
            sch.ApproveContent = "";
            sch.Status = (int)Status.Unfinish;

            ScheduleServer server = new ScheduleServer();
            bool bl = server.InsertSchedules(sch);
            return RedirectToAction("MyScheduleList", "Schedule");
        }

        [HttpPost]
        public ActionResult EditSchedule(Schedules schedule, string tag)
        {
            CheckSession();
            ScheduleServer server = new ScheduleServer();
            string id = Request["id"] ?? string.Empty;
            Schedules s1 = new Schedules();
            if (tag == "edit")
            {
                s1.Status = (int)Status.Unfinish;
            }

            if (tag == "submit")
            {
                s1.Status = (int)Status.Pedding;
            }

            s1.Title = schedule.Title;
            s1.Contents = schedule.Contents;
            s1.Id = schedule.Id;
            s1.ApproveContent = schedule.ApproveContent;
            s1.BeginDate = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
            s1.ApproveDate = schedule.ApproveDate;


            User user = Session["user"] as User;
            s1.SendUid = user.Uid;

            User other = Session["otheruser"] as User;
            s1.ApproveUid = other.Uid;

            bool bl = server.UpdateSchedules(s1);
            return RedirectToAction("MyScheduleList", "Schedule");
        }

        [HttpPost]
        public ActionResult ApproveAction(Schedules schedule)
        {
            CheckSession();
            Schedules sch = schedule;
            sch.ApproveDate = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
            sch.Status = (int)Status.Finish;

            ScheduleServer server = new ScheduleServer();
            bool bl = server.UpdateSchedules(sch);

            return RedirectToAction("MyScheduleList", "Schedule");
        }

        [HttpPost]
        public ActionResult RefuseSchedule(Schedules schedule)
        {
            CheckSession();
            Schedules sch = schedule;
            sch.ApproveDate = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
            sch.Status = (int)Status.Unfinish;

            ScheduleServer server = new ScheduleServer();
            bool bl = server.UpdateSchedules(sch);

            return RedirectToAction("MyScheduleList", "Schedule");
        }

        public ActionResult SubmitSchedule(Schedules schedule)
        {
            CheckSession();
            ScheduleServer server = new ScheduleServer();
            string id = Request["id"] ?? string.Empty;

            Schedules s1 = new Schedules();
            s1.Title = schedule.Title;
            s1.Contents = schedule.Contents;
            s1.Id = schedule.Id;
            s1.ApproveContent = schedule.ApproveContent;
            s1.BeginDate = schedule.BeginDate;
            s1.ApproveDate = schedule.ApproveDate;
            s1.Status = (int)Status.Pedding;

            User user = Session["user"] as User;
            s1.SendUid = user.Uid;

            User other = Session["otheruser"] as User;
            s1.ApproveUid = other.Uid;

            bool bl = server.UpdateSchedules(s1);
            return RedirectToAction("MyScheduleList", "Schedule");
        }

        public ContentResult DeleteSchedule(string schId)
        {
            ContentResult re = new ContentResult();

            ScheduleServer server = new ScheduleServer();
            server.DeleteSchedule(int.Parse(schId));
            re.Content = "删除成功";

            return re;
        }
    }
}
